import fetch from 'common/src/utils/fetch'

/**
 * 查询非公选课列表
 */
export function searchCourseOpenList(data) {
    return fetch({
        url: '/api/arrangementservice/NonPublicCourseOpen/findNonPublicCourseOpen',
        method: 'post',
        data: data
    });
}

/**S
 * 批量开课,取消开课
 */
function openOrCloseCourseListRequest(data,isOpen) {
  return fetch({
    url: '/api/arrangementservice/NonPublicCourseOpen/updateIsOpenStatusByIds?isOpen='+isOpen,
    method: 'post',
    data: data
  }); 
}
export async function openOrCloseCourseList(data,isOpen) {
  let size = 20;
  if (data.length <= size) {
    return await openOrCloseCourseListRequest(data,isOpen);
  } else {
    let promiseArr = [];
    let start = 0, end = size;
    do {
      promiseArr.push(openOrCloseCourseListRequest(data.slice(start, end), isOpen));
      start = Math.min(data.length, start + size)
      end = Math.min(data.length, end + size)
    } while(start != end);
    let info = '', result = true;
    let res = await Promise.all(promiseArr);
    res.forEach(element => {
      let {code , msg} = element;
      if (code != 200) {
        info = msg;
      }
      result = result && element.code === 200;
    });
    if (result) {
      return {code: 200};
    } else {
      return {code: 300, msg: info};
    }
  }
    
}

/**
 * 开课或取消开课
 */
export function openOrCloseCourse(id,isOpen) {
    return fetch({
        url: '/api/arrangementservice/NonPublicCourseOpen/updateIsOpenStatus?id='+id+'&isOpen='+isOpen,
        method: 'get'
    });
}

export function deleteCourseList(data){
  return fetch({
    url: '/api/arrangementservice/NonPublicCourseOpen/delete',
    method: 'post',
    data: data
});
}

/**
 *
 * 同步培养方案
 */
export function synchromCourse(calendarId) {
    return fetch({
        url: '/api/arrangementservice/NonPublicCourseOpen/addNonPublicCourseOpenByCulture?calendarId='+calendarId,
        method: 'get',
    });
}

/**
 * 查询专业
 * @param {*} id
 */
export function queryProfession(data) {
    return fetch({
        url: '/api/arrangementservice/NonPublicCourseOpen/findProfessonsById',
        method: 'post',
        data: data
    });
}

/**
 * 查询同步状态
 *
 */
export function getStatus(key) {
    return fetch({
        url: '/api/arrangementservice/NonPublicCourseOpen/findSyndromeStatus?key='+key,
        method: 'get'
    });
}


/*export function exportList(data) {
    return fetch({
        url: '/api/arrangementservice/NonPublicCourseOpen/export',
        method: 'post',
        data:data
    });
}*/

export function exportList(data) {
  return fetch({
    url: '/api/arrangementservice/NonPublicCourseOpen/export',
    method: 'post',
    data:data
  });
}

/**
 * 手动新增开课课程
 *
 */
export function manualAddCourse(data) {
    return fetch({
        url: '/api/arrangementservice/NonPublicCourseOpen/manualAddCourse',
        method: 'post',
        data
    });
}
/*
* 进行传送学院年级专业，联动校区
* */
export function queryCampusList(data) {
  return fetch({
    url: '/api/arrangementservice/professioncampusnum/findProfessionCampusNumByParam',
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    data
  });
}

/**
 * 导出
 * @param {*} params
 */
export function getReportStatus(key) {
  return fetch({
    url: '/api/arrangementservice/NonPublicCourseOpen/result/' + key,
    method: 'get'
  });
}

/*export function queryCampusNum(data) {
  return fetch({
    url: '/api/arrangementservice/NonPublicCourseOpen/queryCampusNum',
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    data
  });
}*/

